package com.wondersgroup.wuhan_demo.util;

import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import com.wondersgroup.wuhan_demo.Constant;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * Created by liyaqing on 2018/5/22 0022.
 */

public class FileUtil {
    public static boolean analyzeFile(Context context) {
        boolean analyze=false;
        File file = null;
        String fileContent="";
        //判断sd卡是否存在
        boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if (sdCardExist) {
            //获得SD卡根目录路径
            String sdpath = Environment.getExternalStorageDirectory().getPath();
            Log.i("sdpath===", sdpath);
            //获取指定图片路径
            String filepath = sdpath + "/robot-service-config.txt";
            Log.i("filepath===", filepath);
            file = new File(filepath);
            if (file.exists()) {
                try {
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    String readline = "";
                    StringBuffer sb = new StringBuffer();
                    while ((readline = br.readLine()) != null) {
                        sb.append(readline);
                    }
                    br.close();
                    Log.i("sb===", sb.toString().trim());
                    fileContent = sb.toString().trim();

                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(context, "robot-service-config.txt文件解析失败！", Toast.LENGTH_SHORT).show();
                }
                try {
                    Document doc = DocumentHelper.parseText(fileContent); // 将字符串转为XML
                    Log.i("doc==", doc.asXML().toString());
                    Element rootElt = doc.getRootElement(); // 获取根节点
                    Log.i("root==", rootElt.getText());
                    Constant.URL = rootElt.elementText("url");
                    Constant.orgCode = rootElt.elementText("orgCode");
                    Constant.userName = rootElt.elementText("userName");
                    Constant.passWord = rootElt.elementText("passWord");
                    analyze=true;
                    Log.i("root==111", Constant.orgCode + "," + Constant.userName + "," + Constant.passWord);
                } catch (DocumentException e) {
                    e.printStackTrace();
                    Toast.makeText(context, "robot-service-config.txt文件节点解析失败！", Toast.LENGTH_SHORT).show();
                }

            } else {
                Toast.makeText(context, "请检查robot-service-config.txt配置！", Toast.LENGTH_LONG).show();
            }
        }else {
            Toast.makeText(context, "sd卡不存在！", Toast.LENGTH_SHORT).show();
        }
        return analyze;
    }
}
